package com.mySampleApplication.client.components;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.CloseClickEvent;
import com.smartgwt.client.widgets.events.CloseClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.HStack;
import com.smartgwt.client.widgets.layout.VLayout;

/**
 * 用法
 *      // 创建按钮和事件处理
 *      Button addButton = new Button("Add");
 *      addButton.setShowRollOver(true);
 *      addButton.setShowDown(true);
 *      addButton.addClickHandler(event -> showAddDialog(listGrid));
 *     private static void showAddDialog(ListGrid listGrid) {
 *       Window dialog=SShowWindow.getShowWindow();
 *       dialog.show();
 *     }
 */
public class SShowWindow {
    public static Window  getShowWindow(int width, int height, String title, final ListGridRecord record, ListGrid listGrid){
        final Window winModal = new Window();
        //winModal.setAutoSize(true);
        winModal.setWidth(width);
        winModal.setHeight(height);
        winModal.setTitle(title);
        winModal.setShowMinimizeButton(false);
        winModal.setIsModal(true);
        winModal.setShowModalMask(true);
        winModal.setCanDragReposition(true);
        winModal.setCanDragResize(true);
        winModal.centerInPage();
        winModal.addCloseClickHandler(new CloseClickHandler() {
            public void onCloseClick(CloseClickEvent event) {
                winModal.destroy();
            }
        });
        DynamicForm form = new DynamicForm();
        form.setPadding(20);
        form.setWidth100();
        form.setHeight(height-100);
        form.setNumCols(5);
        form.setCellPadding(10);

        TextItem nameItem = new TextItem("name", "名称");
        TextItem loginItem = new TextItem("loginName", "登录名");
        TextItem genderItem = new TextItem("gender", "性别");
        TextItem ageItem = new TextItem("age", "年龄");
        TextItem emailItem = new TextItem("email", "邮箱");

        if (record != null) {
            // 编辑模式下，将已有数据显示在表单中
            nameItem.setValue(record.getAttribute("name"));
            loginItem.setValue(record.getAttribute("loginName"));
            genderItem.setValue(record.getAttribute("gender"));
            ageItem.setValue(record.getAttribute("age"));
            emailItem.setValue(record.getAttribute("email"));
        }

        Button saveButton = new Button("Save");
        saveButton.addClickHandler(event -> {
            if (form.validate()) {
                if (record != null) {
                    // 更新已有记录
                    record.setAttribute("name", nameItem.getValueAsString());
                    record.setAttribute("loginName", loginItem.getValueAsString());
                    record.setAttribute("gender", genderItem.getValueAsString());
                    record.setAttribute("age", ageItem.getValueAsString());
                    record.setAttribute("email", emailItem.getValueAsString());
                    listGrid.updateData(record);

                } else {
                    // 添加新记录
                    ListGridRecord newRecord = new ListGridRecord();
                    newRecord.setAttribute("name", nameItem.getValueAsString());
                    newRecord.setAttribute("loginName", loginItem.getValueAsString());
                    newRecord.setAttribute("gender", genderItem.getValueAsString());
                    newRecord.setAttribute("age", ageItem.getValueAsString());
                    newRecord.setAttribute("email", emailItem.getValueAsString());
                    listGrid.addData(newRecord);
                }
                winModal.hide();
            }
        });

        Button cancelButton = new Button("Cancel");
        cancelButton.addClickHandler(event -> winModal.hide());

        form.setItems(nameItem,loginItem,genderItem,ageItem,emailItem);
        winModal.addItem(form);

        HLayout buttonLayout = new HLayout();
        buttonLayout.setMembersMargin(10);
        buttonLayout.setAlign(Alignment.RIGHT);
        buttonLayout.setWidth100();
        buttonLayout.setHeight(30);
        buttonLayout.setPadding(5);
        buttonLayout.setLayoutAlign(VerticalAlignment.BOTTOM);
        buttonLayout.addMembers(saveButton, cancelButton);
        winModal.addItem(buttonLayout);

        HStack layoutTopRow = new HStack(10);
        layoutTopRow.setHeight(50);

        VLayout layoutMain = new VLayout();
        layoutMain.addMember(layoutTopRow);

        layoutMain.draw();
        return winModal;
    }

}
